/*
 * @author Javier A. Ramírez M.
 * jaramirez85@yahoo.es
 */
package com.jav;

import com.jav.bean.Producto;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.jav.componentes.textfield.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.util.Resources;
import java.util.Vector;

/**
 *
 * @author Javier
 */
public class ProductosDialog extends Dialog {

    private static ProductosDialog _instance;
    public static Producto proselect;
    private List lis;
    private int ESPERA = 2000;

    public static ProductosDialog getInstance() {
        if (_instance == null) {
            _instance = new ProductosDialog();
        }
        return _instance;
    }

    private ProductosDialog() {
        setScrollable(false);
        setLayout(new BorderLayout());
        setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 500));
        setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 500));

        lis = createList(Datos.getInstance().getProdOrd(), new ContactsRenderer());
        Resources res = Datos.getInstance().getRes();
        addComponent(BorderLayout.CENTER, lis);
        addCommand(new Command("Adicionar", res.getImage("add.png")) {

            public void actionPerformed(ActionEvent evt) {
                proselect = (Producto) lis.getSelectedItem();
            }
        });
        addCommand(new Command("Cancelar", res.getImage("cancel.png")) {

            public void actionPerformed(ActionEvent evt) {
                ProductosDialog.proselect = null;
                dispose();
            }
        });
    }

    private List createList(Vector productos, ListCellRenderer renderer) {
        List list = new List(productos) {

            private long lastSearchInteraction;
            private TextField search = new TextField(3);

            {
                search.getStyle().setBgTransparency(255);
                search.setReplaceMenu(false);
                search.setInputModeOrder(new String[]{"123"});
                search.setFocus(true);
            }

            public void keyPressed(int code) {
                try {
                    int game = Display.getInstance().getGameAction(code);
                    if (game > 0) {
                        super.keyPressed(code);
                    } else {
                        search.keyPressed(code);
                        lastSearchInteraction = System.currentTimeMillis();
                    }
                } catch (Exception e) {
                }
            }

            public void keyReleased(int code) {
                int game = Display.getInstance().getGameAction(code);
                if (game > 0) {
                    super.keyReleased(code);
                } else {
                    search.keyReleased(code);
                    lastSearchInteraction = System.currentTimeMillis();
                    String t = search.getText();
                    int modelSize = getModel().getSize();
                    for (int iter = 0; iter < modelSize; iter++) {
                        String v = ((Producto) getModel().getItemAt(iter)).getCodigo();
                        //String v = "";
                        if (v.startsWith(t)) {
                            setSelectedIndex(iter);
                            return;
                        }
                    }
                }
            }

            public void paint(Graphics g) {
                super.paint(g);
                if (System.currentTimeMillis() - ESPERA < lastSearchInteraction || search.isPendingCommit()) {
                    search.setSize(search.getPreferredSize());
                    Style s = search.getStyle();
                    search.setX(getX() + getWidth() - search.getWidth() - s.getPadding(RIGHT) - s.getMargin(RIGHT));
                    search.setY(getScrollY() + getY());
                    search.paintComponent(g, true);
                }
            }

            public boolean animate() {
                boolean val = super.animate();
                if (lastSearchInteraction != -1) {
                    search.animate();
                    if (System.currentTimeMillis() - ESPERA > lastSearchInteraction && !search.isPendingCommit()) {
                        lastSearchInteraction = -1;
                        search.setText("");//clear();
                    }
                    return true;
                }
                return val;
            }
        };
        list.setListCellRenderer(renderer);
        list.setOrientation(List.VERTICAL);

        return list;
    }
}

class ContactsRenderer extends Container implements ListCellRenderer {

    private Label lb_cod_des = new Label("");
    private Label focus = new Label("");

    public ContactsRenderer() {
        setLayout(new BorderLayout());
        addComponent(BorderLayout.CENTER, lb_cod_des);
        lb_cod_des.setFocusable(true);
        focus.setFocusable(true);

    }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {

        Producto p = (Producto) value;
        lb_cod_des.setText(p.getCodigo() + " - " + p.getDescripcion());
        if (isSelected) {
            lb_cod_des.getStyle().setBgColor(0x08088A);
            lb_cod_des.getStyle().setFgColor(0xffffff);
        } else {
            lb_cod_des.getStyle().setBgColor(0xE6FF00);
            lb_cod_des.getStyle().setFgColor(0x000000);
        }
        return this;
    }

    public Component getListFocusComponent(List list) {
        return focus;
    }
}
